﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kugar.Core.PluginFramework.Common;

namespace Kugar.Core.PluginFramework.Host.Test
{
    static  class Program
    {
        static async Task Main(string[] args)
        {
            pathLoadTest();

            Console.ReadLine();
        }

        private static void Host_PluginExecuteError(string pluginName, IPlugin plugin, string funcName, IEnumerable<KeyValuePair<string, object>> args, Exception error)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 插件路径修改后测试
        /// </summary>
        private static async void pathLoadTest()
        {
            var host=new PluginHost(pluginLoadPath: "Plugins");

            var result=host.Execute<string>("MongoState", "GetAdminFromDB", null);

        }

        /// <summary>
        /// 基础功能测试
        /// </summary>
        private static async void baseTest()
        {
            var host = new PluginHost(typeof(SyncPluginBase));

            var host2 = new PluginHost(typeof(WebApiPluginBase));

            var host3 = new PluginHost();

            host.PluginExecuteError += Host_PluginExecuteError;

            var value = host.Execute("PluginTest", "Test1", new Dictionary<string, object>()
            {
                ["arg1"] = "sss",
                ["arg2"] = 2
            });

            try
            {
                //测试使用MethodNameAttrbute特性修改函数名
                var value5 = host.Execute("PluginTest", "Test5", new Dictionary<string, object>()
                {
                    ["arg1"] = "sss",
                    ["arg2"] = 2
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }


            try
            {
                var value2 = host.Execute("PluginTest", "TestError", new Dictionary<string, object>()
                {
                    ["arg1"] = "sss",
                    ["arg2"] = 2
                });
            }
            catch (Exception e)
            {
                //Console.WriteLine(e);
                //throw;
            }


            //有异步返回值
            var value3 = await host.ExecuteAsync<string>("PluginTest", "Test1Async", new Dictionary<string, object>()
            {
                ["arg1"] = "sss",
                ["arg2"] = 2
            });

            //无异步返回值
            await host.ExecuteAsync("PluginTest", "Test1Async2", new Dictionary<string, object>()
            {
                ["arg1"] = "sss",
                ["arg2"] = 2
            });

            //忽略异步返回值
            await host.ExecuteAsync("PluginTest", "Test1Async", new Dictionary<string, object>()
            {
                ["arg1"] = "sss",
                ["arg2"] = 2
            });

            //异步方法调用同步函数,不会出错
            var value4 = await host.ExecuteAsync<string>("PluginTest", "Test1", new Dictionary<string, object>()
            {
                ["arg1"] = "sss",
                ["arg2"] = 2
            });

        }
    }

    public abstract class WebApiPluginBase : PluginBase
    {

    }

    public abstract class SyncPluginBase : PluginBase
    {

    }

    public class PluginTest: SyncPluginBase
    {
        public override int Version { get; }
        public override string PluginName => "PluginTest";

        public string Test1(string arg1,int arg2)
        {
            return $"arg1={arg1},arg2={arg2}";
        }

        public string TestError(string arg1, int arg2)
        {
            throw new Exception("TestError");
        }

        public async Task<string> Test1Async(string arg1, int arg2)
        {
            await Task.Delay(2000);

            return  $"arg1={arg1},arg2={arg2}";
        }

        public async Task Test1Async2(string arg1, int arg2)
        {
            await Task.Delay(2000);

            //return $"arg1={arg1},arg2={arg2}";
        }

        //测试使用特性修改函数名称
        [MethodName("Test5")]
        public string Test3(string arg1, int arg2)
        {
            return $"arg1={arg1},arg2={arg2}";
        }
    }

    /// <summary>
    /// 共享实例的插件
    /// </summary>
    [SharedInstance]
    public class SharedInstancePluginTest : SyncPluginBase
    {
        public override int Version { get; }
        public override string PluginName => "SharedInstancePluginTest";

        public SharedInstancePluginTest()
        {

        }
    }

    /// <summary>
    /// 测试忽略指定插件
    /// </summary>
    [IgoneExport]
    public class IgoneExportPluginTest : WebApiPluginBase
    {
        public override int Version { get; }
        public override string PluginName => "IgoneExportPluginTest";

        public IgoneExportPluginTest()
        {

        }
    }
}
